﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing;
using System.Windows;
using Microsoft.Win32;
using SecureCommander.Core;
using SecureCommander.Helper;

namespace SecureCommander.GUI
{
    /// <summary>
    /// Interaction logic for ucExplorer.xaml
    /// </summary>
    public partial class UCExplorer : System.Windows.Controls.UserControl
    {

        #region Properties

        private bool isRightClickOnFile = false;
        private string _currentDirectory;

        public String CurrentDirectory
        {
            get
            {
                return _currentDirectory;
            }
            set
            {
                _currentDirectory = value;
                if (!_currentDirectory.EndsWith("\\"))
                {
                    _currentDirectory += "\\";
                }
                txtPath.Text = _currentDirectory;
                if (MainForm != null)
                {
                    MainForm.CachePaths[CurrentPartition] = _currentDirectory;
                }
            }
        }
        public UCExplorer OtherExplorer { get; set; }
        public MainWindow MainForm { get; set; }
        public string CurrentPartition { get; set; }

        #endregion

        #region Methods

        public UCExplorer()
        {
            InitializeComponent();
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            lvContent.ContextMenu = (ContextMenu)lvContent.Resources["RightClickContextMenu"];
        }

        public void InitPath()
        {
            string[] disks = Environment.GetLogicalDrives();
            
            cmbHardDisk.ItemsSource = new CollectionView(disks);

            
            if (!Directory.Exists(MainForm.CachePaths[CurrentPartition].ToString()))
            {
                cmbHardDisk.SelectedIndex = 0;
                CurrentPartition = Environment.GetLogicalDrives()[0];
                CurrentDirectory = Environment.GetLogicalDrives()[0];
            }
            else
            {
                int index = 0;
                foreach (string disk in disks)
                {
                    if (disk == CurrentPartition)
                    {
                        cmbHardDisk.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
                CurrentDirectory = MainForm.CachePaths[CurrentPartition].ToString();
            }            
        }

        private void Navigate()
        {
            if (Directory.Exists(txtPath.Text))
            {
                CurrentDirectory = txtPath.Text;
                FileHelper.DisplayExplorer(this);
            }
            else
            {
                FileHelper.ShowMessage("Folder does not exists!", MessageBoxImage.Stop);
            }
        }

        #endregion

        #region Event Handlers

        private void lvContent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            ListViewContent item = (ListViewContent)((FrameworkElement)e.OriginalSource).DataContext;
            if (item != null)
            {
                FileHelper.OpenItem(this, (ListViewContent)((ListView)sender).SelectedItem);
            }
        }

        private void lvContent_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            ListViewContent item = (ListViewContent)((FrameworkElement)e.OriginalSource).DataContext;
            if (item != null)
            {
                // Right click on item
                if (((ListViewContent)lvContent.SelectedItem).Name != "...")
                {
                    if (!isRightClickOnFile)
                    {
                        lvContent.ContextMenu = (ContextMenu)lvContent.Resources["FileFolderRightClickContextMenu"];
                        isRightClickOnFile = true;
                    }
                }
            }
            else
            {
                if (isRightClickOnFile)
                {
                    lvContent.ContextMenu = (ContextMenu)lvContent.Resources["RightClickContextMenu"];
                    isRightClickOnFile = false;
                }
            }
        }

        private void mnNewFolder_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.NewFolder(this);
        }

        private void mnNewTxt_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.NewTextDocument(this);
        }

        private void mnNewDoc_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.NewWordDocument(this);
        }

        private void mnRefresh_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.Refresh(this);
        }

        private void mnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.SelectedAll(this);
        }

        private void mnOpen_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.OpenItem(this, (ListViewContent)lvContent.SelectedItem);
        }

        private void mnCopy_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.Copy(this, OtherExplorer);
        }

        private void mnDelete_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.Delete(this);
        }

        private void mnRename_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.Rename(this);
        }

        private void mnPack_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.Pack(this);
        }
        
        private void mnUnPack_Click(object sender, RoutedEventArgs e)
        {
            FileHelper.UnPack(this);
        }

        private void mnProperties_Click(object sender, RoutedEventArgs e)
        {
            NativeMethods.ShowFileProperties(((ListViewContent)lvContent.SelectedItem).Tag.FullName);
        }

        private void cmbHardDisk_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CurrentDirectory != null)
            {
                CurrentPartition = e.AddedItems[0].ToString();
                CurrentDirectory = MainForm.CachePaths[CurrentPartition].ToString();
                FileHelper.DisplayExplorer(this);
            }
        }

        private void txtPath_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    Navigate();
                    e.Handled = true;
                    break;
                case Key.Back:
                    e.Handled = true;
                    break;
                case Key.Delete:
                    e.Handled = true;
                    break;
                default:
                    break;
            }

        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            Navigate();
        }

        private void mnDecrypt_Click(object sender, RoutedEventArgs e)
        {
            MainForm.mnDecrypt_Click(sender, e);
        }

        private void mnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            MainForm.mnEncrypt_Click(sender, e);
        }

        private void mnSign_Click(object sender, RoutedEventArgs e)
        {
            MainForm.mnSign_Click(sender, e);
        }

        private void mnVerifySignature_Click(object sender, RoutedEventArgs e)
        {
            MainForm.mnVerify_Click(sender, e);
        }

        #endregion


    }
}
